$(document).ready(function(){
	vm.load();
	$("#page_title").html("编辑运动");
});
var vm = avalon.define({
	//标记是否显示遮罩
	is_mask_show:false,	
	$id:"main_controller",
	//----------------
	//toast的信息
	toast_msg:'',
	//是否要展示toast
	is_toast_show:false,
	//定时器
	$timeout:0,
	//消息隐藏
	clear_toast:function(){
		//设置定时器
		if(vm.$timeout){
			clearTimeout(vm.$timeout);
		}
		vm.$timeout = setTimeout(function(){
			vm.is_toast_show = false;
			vm.toast_msg = '';
		},3000);		
	},	
	//-----------------
	//操作对象
	sport:{
		//开始时间
		begindate:'',
		//运动时间
		time:'',
		//名称
		name:'',
	},
	//消耗的能量
	energy:{
		get:function(){
			if(!this.sport.exercisetime || volidate_js.is_num(this.sport.exercisetime)==false){
				return '';
			}
			return (this.sport.exercisetime*this.danwei).toFixed(2);
		},
	},
	//每消耗
	danwei:'',
	sport:{
		createdate: "",
		dictexercise:"",
		dictmemberid: "",
		exercisedate:"",
		exercisetime: "",
		expendenergy: "",
		memdailyfoodid: "",
		numrow: "",
	},
	//提交
	submit:function(){
		//验证
		if(!vm.sport.exercisetime.trim()){
			vm.toast_msg = "请输入时间";
			vm.is_toast_show = true;
			vm.clear_toast();					
			return ;
		}
		if(volidate_js.is_num(vm.sport.exercisetime.trim())==false){
			vm.toast_msg = "分钟格式有误";
			vm.is_toast_show = true;
			vm.clear_toast();					
			return ;			
		}
		var fd = new FormData();
		fd.append('id',common.getUrlParam('id'));
		fd.append('time',vm.sport.exercisetime);
		fd.append('energy',vm.energy);
		var $this = $(this);
		$this.button('loading');
		$.ajax({
			type:"post",
			url:save_member_exercise_url,
			dataType:"json",
			data:fd,
			processData: false,
			contentType: false,					
			success:function(data){
				$this.button('reset');
				if(data.status == 1){
					vm.toast_msg = "操作成功";
					setTimeout(function(){
						history.go(-1);
					},1000);
				}else if(data.status == 2){
					vm.toast_msg = "操作失败";
				}else{
					vm.toast_msg = "服务器异常";
				}
				vm.is_toast_show = true;
				vm.clear_toast();
			},
			error:function(){
				$this.button('reset');
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();					
			},	
		});
	},
	//加载数据
	load:function(){
		vm.is_mask_show = true;
		$.ajax({
			type:"post",
			url:get_member_exercise_info_url,
			dataType:"json",
			data:"id="+common.getUrlParam('id'),
			success:function(data){
				vm.is_mask_show = false;
				console.log(data);
				vm.sport = data;
				var danwei = data.expendenergy/data.exercisetime;
				vm.danwei = danwei.toFixed(2);
			},
			error:function(){
				vm.is_mask_show = false;
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();					
			},	
		});		
	},
	//追加变量防止重复提交
	$is_do:false,	
	delete_fun:function(){
		if(vm.$is_do){
			return ;
		}		
		var $this = $(this);
		$this.button('loading');
		$.ajax({
			type:"post",
			url:delete_member_exercise_url,
			dataType:"json",
			data:"id="+common.getUrlParam("id")+"&date="+common.getUrlParam("date"),
			success:function(data){
				$this.button('reset');
				if(data.status == 1){
					vm.$is_do = true;
					vm.toast_msg = "操作成功";
					setTimeout(function(){
						history.go(-1);
					},1000);
				}else if(data.status == 2){
					vm.toast_msg = "操作失败";
				}else{
					vm.toast_msg = "服务器异常";
				}
				vm.is_toast_show = true;
				vm.clear_toast();
			},
			error:function(){
				$this.button('reset');
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();					
			},	
		});		
	},
});
avalon.scan();
avalon.ready(function(){
	$('body').css('opacity',1);
});
